const { merge } = require('webpack-merge')
const common = require('./webpack.common')
const path = require('path')
const Dotenv = require('dotenv-webpack');
const webpack = require('webpack')

module.exports = (env = {}) => {
    console.log('env.NODE_ENV', env.NODE_ENV)
    return merge(common, {
        cache: {
            type: 'filesystem', // 使用文件系统缓存
            // 可选配置
            name: 'dev-cache', // 缓存名称
            cacheDirectory: path.resolve(__dirname, '.temp_cache'), // 缓存存放路径
            allowCollectingMemory: true,
            compression: 'gzip'
        },
        optimization: {
            splitChunks: {
                chunks: 'async',
                maxSize: 50000
            }
        },
        plugins: [
            new Dotenv({
                path: path.resolve(__dirname, `../.env.${env.NODE_ENV || 'development'}`),
                systemvars: true // 同时加载系统环境变量
            }),
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV || 'development'),
            })
        ],
        devtool: 'source-map',
        mode: env.NODE_ENV || 'development'
    })
}